/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\mfileconfig.h"

MFileConfig::MFileConfig(void)
{
	idLen = 0;
}

MFileConfig::~MFileConfig(void)
{
}

//---------------------------------------------------------------------------

bool MFileConfig::BoolFromString(CString strIn)
{
	CString str;

	str = strIn.Trim();
	if(str.CompareNoCase("true")==0) return true;
	if(str.CompareNoCase("yes")==0)  return true;

	return false;
}

CString MFileConfig::BoolToString(bool val)
{
	CString str;

	str = val ? "true" : "false";
	return str;
}

//---------------------------------------------------------------------------

int MFileConfig::IntFromString(CString strIn)
{
	return _tstoi(strIn);
}

CString MFileConfig::IntToString(int val)
{
	CString str;

	str.Format(_T("%d"), val);
	return str;
}

//---------------------------------------------------------------------------

bool MFileConfig::RectFromString(CRect* rectIn, CString strIn)
{
	CString str;
	int aa, bb, cc, dd;

	if(rectIn==NULL) return false;

	if(strIn.GetLength()!=23)
		return true;

	str = strIn.Left(5);
	aa  = _tstoi(str);
	str = strIn.Mid(6, 5);
	bb  = _tstoi(str);
	str = strIn.Mid(12, 5);
	cc  = _tstoi(str);
	str = strIn.Right(5);
	dd  = _tstoi(str);

	rectIn->left   = aa;
	rectIn->top    = bb;
	rectIn->right  = cc;
	rectIn->bottom = dd;

	return true;
}

CString MFileConfig::RectToString(CRect* rectIn)
{
	CString str;

	str.Empty();
	if(rectIn==NULL) return str;

	str.Format(_T("%05d %05d %05d %05d"), rectIn->left, rectIn->top, rectIn->right, rectIn->bottom);

	return str;
}

//---------------------------------------------------------------------------

bool MFileConfig::WriteRawData(BYTE* dataIn, unsigned int sizeIn)
{
	bool res;

	if(dataIn==NULL) return false;

	res = true;
	try{
		theFile.Write(dataIn, sizeIn);
	}
	catch(...){
		res = false;
	}

	return res;
}

bool MFileConfig::WriteString(CString strIn)
{
	bool res;

	res = true;
	try{
		theFile.WriteString(strIn);
	}
	catch(...){
		res = false;
	}

	return res;
}

bool MFileConfig::WriteEmptyLine(void)
{
	CString str;

	str = _T("\n");
	return WriteString(str);
}

bool MFileConfig::WriteCommentLine(CString strIn)
{
	CString str;

	str  = _T("# ");
	str += strIn;
	str += _T("\n");
	return WriteString(str);
}

bool MFileConfig::WriteEmptyCommentLine(void)
{
	CString str;

	str  = _T("#\n");
	return WriteString(str);
}

bool MFileConfig::AddTimeStampAsComment(void)
{
	CString str;
	time_t now;
	char timeBuff[27];
	errno_t err;

	str = _T("# ");
	_tzset();
	time(&now);

	memset(timeBuff, 0, 27);
	err = ctime_s(timeBuff, 27, &now);

	if(err==0)
		str += timeBuff;
	return WriteString(str);
}

bool MFileConfig::WriteLine(CString strIn)
{
	CString str;

	str = strIn;
	str += _T("\n");
	return WriteString(str);
}

bool MFileConfig::WriteLineWithId(CString idIn, CString strIn)
{
	CString str;

	str = idIn;
	if(idLen>0){
		while(str.GetLength()<idLen)
			str += _T(" ");
	}

	str += _T(" = ");

	str += strIn;

	str += _T("\n");

	return WriteString(str);
}

bool MFileConfig::SaveError(void)
{
	theFile.Flush();

	theFile.Close();

	return false;
}

//---------------------------------------------------------------------------

void MFileConfig::TrimString(std::string &strIn)
{
	std::string::size_type pos1;
	std::string::size_type pos2;

	pos1 = strIn.find_first_not_of(" \t");
	if(pos1==std::string::npos){
		// empty string of whitespaces only
		strIn = "";
		return;
	}

	pos2 = strIn.find_last_not_of(" \t");
	// pos2 != std::string::npos because otherwise pos1==std::string::npos
	// and the execution could not reach this point

	pos2++;

	if(pos1==0 && pos2==strIn.length()) return; // no trim to be done

	strIn.substr(pos1, pos2 - pos1);
}

//---------------------------------------------------------------------------

bool MFileConfig::LoadFromFile(CString fileNameIn)
{
	std::fstream fs;
	std::string line;
	bool err;

	InternalInit();

	fs.open((char*)(LPCSTR)fileNameIn, std::ios_base::in);
	if(fs.fail())
		return false;

	err = false;
	while(!fs.eof() && !err){
		std::getline(fs, line, '\n');
		if(fs.fail()){
			if(!fs.eof()){
				// reading error
				err = true;
			}
		}
		else{
			TrimString(line);
			if(line.length()>0)
				if(line[0]!='#')
					err = !InternalProcessLine((char*)line.c_str());
		}
	}

	fs.close();

	return !err;
}

//---------------------------------------------------------------------------

bool MFileConfig::SaveToFile(CString fileNameIn)
{
	BOOL res;

	res = theFile.Open(fileNameIn, CFile::modeCreate | CFile::modeWrite);
	if(!res) return false;

	if(!InternalSave())
		return SaveError();

	theFile.Flush();

	theFile.Close();

	return true;
}

//---------------------------------------------------------------------------

void MFileConfig::InternalInit(void)
{
}

bool MFileConfig::InternalProcessLine(char*)
{
	return true;
}

bool MFileConfig::InternalSave(void)
{
	return true;
}

//---------------------------------------------------------------------------
